﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_PrintPurList : System.Web.UI.Page
{
    Int64 PPHospitalId;
    String PPHospitalName;
    String PPProjectId;
    String PPAccountid_TB;
    String PPAccountid_PS;
    String Id;
    String _Goodsid;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                if (_Account.Type == "3")
                {
                    PPHospitalId = SysFun.ToLong(_Account.AccountId);
                    PPHospitalName = _Account.DWName;
                }
                else
                {
                    PPHospitalId = SysFun.ToLong(SysFun.IsSQL(Request.QueryString["Accountid_YY"]));
                    PPHospitalName = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["HospitalName"]));
                }
                PPProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"]);
                PPAccountid_TB = SysFun.IsSQL(Request.QueryString["Accountid_TB"]);
                PPAccountid_PS = SysFun.IsSQL(Request.QueryString["Accountid_PS"]);
                Id = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["Id"]));
                _Goodsid = SysFun.IsSQL(Request.QueryString["goodsid"]);
                if (Id.Equals("1") || Id.Equals("2")) 
                {
                    Button1.Visible = false;
                }
                BindData();

            }
        }

    }

    public void BindData()
    {
        String StrSql, _StrWhere;

        if (PPAccountid_PS == "")
        {
            if (Id.Equals("1"))//中标人查看合同
            {
                _StrWhere = " Where projectid="+PPProjectId+" and AccountId_TB=" + PPAccountid_TB + " And State='正常采购' and AccountId_YY=" + PPHospitalId.ToString() + " And";
            }
            else if (Id.Equals("2"))//配送人查看合同
            {
                _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_PS=" + PPAccountid_PS + " And State='正常采购' And AccountId_YY=" + PPHospitalId.ToString() + " And";
            }
            else
            {
                if ((_Goodsid != "")&&(_Goodsid!=null))
                {
                    _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId + "' And AccountId_TB=" + PPAccountid_TB + " and usercode_ps is null and State='正常采购' And goodsid=" + _Goodsid + " and";
                }
                else if ((PPAccountid_TB != "") && (PPAccountid_TB != ""))
                {
                    _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId + "' And AccountId_TB=" + PPAccountid_TB + " and usercode_ps is null and State='正常采购' And";
                }
                else
                {
                    _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId + "' and State='正常采购' And";
                }
            }
        }
        else if (PPAccountid_TB == "")
        {
            _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId.ToString() + "' And Accountid_PS=" + PPAccountid_PS + " And State='正常采购' And";
        }
        else 
        {
            if ((_Goodsid != "")&&(_Goodsid!=null))
            {
                _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId.ToString() + "' And Accountid_TB=" + PPAccountid_TB + " And Accountid_PS=" + PPAccountid_PS + " And State='正常采购' And goodsid=" + _Goodsid + " and";
            }
            else
            {
                _StrWhere = " Where projectid=" + PPProjectId + " and AccountId_YY='" + PPHospitalId.ToString() + "' And Accountid_TB=" + PPAccountid_TB + " And Accountid_PS=" + PPAccountid_PS + " And State='正常采购' And";
            }
        }
        if (_StrWhere != "")
        {
            _StrWhere = "  " + _StrWhere.Substring(0, _StrWhere.Length - 3);
        }
        Db db = new Db();
        System.Data.IDataReader dr;
        StrSql = "Select CompanyName_TB,GoodsId,ProductName,GoodsName,MedicineModel,Outlookc,Factor,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,PurchasePrice,accountid_sc,UserCode_SC,CompanyName_SC,CompanyName_TB,UserCode_TB,HospitalName,accountid_ps,UserCode_PS,CompanyName_PS,BidPrice,PlanPurNum,convert(numeric(30,2),PurchasePrice*PlanPurNum) as PlanPrice From PurchaseTable";
        StrSql += _StrWhere;
        dr = db.DataReader(StrSql);
        this.GridView1.DataSource = dr;
        this.GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            if (_Account.Type == "3")
            {
                Db db = new Db();
                System.Data.IDataReader dr;
                dr = db.DataReader("select count(*) as id from hospital_contract_print where  Project_Id=" + PPProjectId + " and  hospital_id=" + PPHospitalId + " and Company_ps_id=" + PPAccountid_PS);
                dr.Read();
                if (SysFun.ToInt(dr["id"]) != 0)
                {

                }
                else
                {
                    dr.Close();
                    db.Command("insert into hospital_contract_print values( " + PPHospitalId + "," + PPProjectId + "," + PPAccountid_PS + ",1)");
                }
                dr.Close();
                dr.Dispose();
                db.Close();
            }
        }
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(TextBox1.Text.ToString()) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        string  strHtml = "";
        strHtml = oStringWriter.ToString().Insert(oStringWriter.ToString().IndexOf("</table>") + 9, "<table><tr border=\"0\"><td colspan='19'>&nbsp;</td></tr><tr style=\"border-width:thin\"><td colspan=\"2\">&nbsp;</td><td  colspan=\"2\">投标人签章：</td><td colspan=\"2\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td colspan=\"2\">配送企业签章：</td><td colspan=\"2\">&nbsp;</td><td>&nbsp;</td><td>医院签章：</td><td colspan=\"2\">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>");
        Response.Write(strHtml.ToString());
        // 停止页面的执行

        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }

}
